/*
 * Copyright 2014 Davy Maddelein.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.pepshell.view.dataloading;

import com.compomics.pepshell.DataModeController;
import com.compomics.pepshell.model.AnnotatedFile;
import com.compomics.pepshell.view.components.JFileChooserWithMemory;

import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;

/**
 *
 * @author Davy Maddelein
 */
public class ExperimentFileSelectionPanel extends javax.swing.JPanel {

    public static final int ALL_EXPERIMENTS = 0, REFERENCE_EXPERIMENT = 1;

    private DefaultListModel<AnnotatedFile> listModel = new DefaultListModel<>();
    private AnnotatedFile referenceFile;
    private File previousLocation;

    /**
     * Creates new form ExperimentFileBasedExperimentSelectionPanel
     */
    public ExperimentFileSelectionPanel() {
        initComponents();
    }

    public void setFiles(List<AnnotatedFile> aList) {
        listModel.removeAllElements();
        aList.stream().forEach((aFile) -> {
            listModel.addElement(aFile);
        });
    }

    public void setFiles(AnnotatedFile aReferenceFile, List<AnnotatedFile> aList) {
        listModel.removeAllElements();
        referenceFile = aReferenceFile;
        previousLocation = referenceFile.getParentFile();
        listModel.addElement(referenceFile);
        aList.stream().forEach((aFile) -> {
            listModel.addElement(aFile);
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton5 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new JList(listModel);

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(374, 115));
        setPreferredSize(new java.awt.Dimension(374, 115));

        jButton5.setText("select comparison experiment(s)");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton2.setText("remove selected comparison experiment(s)");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jList1KeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        JFileChooserWithMemory chooser = JFileChooserWithMemory.getInstance();
        chooser.setMultiSelectionEnabled(true);
        int result = chooser.showOpenDialog(this);
        if (chooser.getSelectedFiles().length > 0 && result == JFileChooser.APPROVE_OPTION) {
            for (File aFile : chooser.getSelectedFiles()) {
                listModel.addElement(new AnnotatedFile(aFile.getAbsolutePath()));
            }
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        jList1.getSelectedValuesList().stream().forEach((aSelectedFile) -> {
            listModel.remove(listModel.indexOf(aSelectedFile));
        });
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jList1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyTyped
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            for (File aSelectedFile : jList1.getSelectedValuesList()) {
                jList1.remove((listModel).indexOf(aSelectedFile));
            }
        }
    }//GEN-LAST:event_jList1KeyTyped

    public List<AnnotatedFile> getExperimentFiles() {
        AnnotatedFile[] tempArray = new AnnotatedFile[listModel.size()];
        listModel.copyInto(tempArray);
        List<AnnotatedFile> files = Arrays.asList(tempArray);
        if (referenceFile != null) {
            files.remove(referenceFile);
        }
        return files;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JList<File> jList1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    public void setMode(int viewMode) {
        switch (viewMode) {
            case ALL_EXPERIMENTS:
                jButton5.setText("select comparison experiment(s)");
                jButton2.setText("remove selected comparison experiment(s)");
                break;
            case REFERENCE_EXPERIMENT:
                jButton5.setText("select experiment(s)");
                jButton2.setText("remove selected experiment(s)");
                break;

        }
    }

    public void setSelectionPanelLocation(File location) {
        previousLocation = location;
    }
}
